package com.ruoyi.web.controller.admin.msg;

import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.system.service.ProjectMsgTemplateAdminService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 项目-消息模板 绑定管理（管理员）
 */
@RestController
@RequestMapping("/admin/project/msg-template")
public class ProjectMsgTemplateController extends BaseController {

    @Autowired
    private ProjectMsgTemplateAdminService adminService;

    /**
     * 绑定/更换 项目在某消息类型下的模板（会自动停用历史启用绑定）
     */
    @PostMapping("/bind")
//    @PreAuthorize("@userType.isSys() && @ss.hasRole('admin')")
    public AjaxResult bind(@RequestParam Long projectId,
                           @RequestParam Long templateId,
                           @RequestParam Integer type) {
        adminService.bind(projectId, templateId, type, getUsername());
        return AjaxResult.success("绑定成功");
    }

    /**
     * 停用 项目在某消息类型下的启用绑定
     */
    @PostMapping("/disable")
//    @PreAuthorize("@userType.isSys() && @ss.hasRole('admin')")
    public AjaxResult disable(@RequestParam Long projectId,
                              @RequestParam Integer type) {
        adminService.disable(projectId, type, getUsername());
        return AjaxResult.success("停用成功");
    }

    /**
     * 查询 项目在某消息类型下的启用模板ID
     */
    @GetMapping("/active")
//    @PreAuthorize("@userType.isSys() && @ss.hasRole('admin')")
    public AjaxResult getActive(@RequestParam Long projectId,
                                @RequestParam Integer type) {
        Long templateId = adminService.getActiveTemplateId(projectId, type);
        return AjaxResult.success().put("templateId", templateId);
    }

    /**
     * 查询 指定模板绑定的项目ID列表
     */
    @GetMapping("/projects")
//    @PreAuthorize("@userType.isSys() && @ss.hasRole('admin')")
    public AjaxResult getProjectsByTemplate(@RequestParam Long templateId) {
        List<Long> projectIds = adminService.getProjectIdsByTemplateId(templateId);
        return AjaxResult.success().put("projectIds", projectIds);
    }
}


